<?php
/**
 * 登录注册模型类
 * @author     fanlc<lingchao.fan@huatandm.com>
 * @copyright  Copyright (c) 2013,上海华探通讯技术有限公司
 * @filesource
 */
defined('IN_RCPHP') or exit('Access denied');

class loginModel extends RcModel
{

	/**
	 * 构造方法 继承基类的构造方法
	 * @author fanlc<lingchao.fan@huatandm.com>
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Validate login info.
	 *
	 * @author fanlc<lingchao.fan@huatandm.com>
	 * @param string $username
	 * @return bool|array
	 */
	public function validateLogin($username)
	{
		if(empty($username))
		{
			return false;
		}

		return $this->from('user', 'uid,password,isEnable')
					->where("isDel = 0 and (username = '" . $username . "' or email = '" . $username . "')")
					->fetchRow();
	}

	/**
	 * check user password.
	 *
	 * @author fanlc<lingchao.fan@huatandm.com>
	 * @param $uid
	 * @param $password
	 * @return bool
	 */
	public function checkUserPassword($uid, $password)
	{
		if(empty($uid) || empty($password))
		{
			return false;
		}

		return $this->from('user', 'uid')
					->where('uid=' . intval($uid) . ' AND password="' . $password.'"')
					->fetchColumn();
	}

	/**
	 * Create user.
	 *
	 * @author fanlc<lingchao.fan@huatandm.com>
	 * @param  array $data
	 * @return int
	 */
	public function createUser($data)
	{
		if(empty($data))
		{
			return false;
		}

		$uid = $this->insert('user', $data, true);
		if(!empty($uid))
		{
			$cateData['uid'] = $uid;
			$cateData['cateName'] = '我的好友';
			$cateData['cateContent'] = '我的好友';
			$cateData['cateIsDefault'] = 1;

			$this->insert('person_category', $cateData);
		}

		return $uid;
	}

	/**
	 * Insert user login log info.
	 *
	 * @author fanlc<lingchao.fan@huatandm.com>
	 * @param array $data
	 * @return bool
	 */
	public function setUserLoginLogInfo($data = array())
	{
		if(empty($data))
		{
			return false;
		}

		return $this->insert('user_login_log', $data);
	}
}